﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PS.FlowDesigner;
using PS.FlowDesigner.Forms;
using PS.FlowDesigner.Printing;
using pswb.Classes;

namespace pswb.Forms.FlowDesigner
{
    public partial class frmFlowDesigner : Form
    {
        private Shape startPoint = null;
        private Shape endPoint = null;
        private Shape errorPoint = null;

        frmPointSelector addAction = null;

        #region Properties

        private string FlowName { get; set; }
        private string ProcessName { get; set; }
        private int ProcessID { get; set; }
        private string Description { get; set; }

        #endregion

        public frmFlowDesigner()
        {
            InitializeComponent();
        }

        private void frmFlowDesigner_Load(object sender, EventArgs e)
        {
            FlowDiagram.Paging.Enabled = false;

            FlowDiagram.Model.SetSize(new SizeF(SystemInformation.VirtualScreen.Width, 1000));
            BasicStencil _bs = new BasicStencil();
            startPoint = new Shape(_bs[BasicStencilType.Circle]);
            startPoint.Width = 30;
            startPoint.Height = 30;
            startPoint.AllowScale = false;
            startPoint.AllowSnap = true;
            startPoint.ApplyTheme(Singleton.Instance.GetTheme(Themes.Green));
            startPoint.Move(10, 10);
            startPoint.Tag = "Start";
            PS.FlowDesigner.Image img = new PS.FlowDesigner.Image(Properties.Resources.step_start);
            img.Location = new PointF(4, 4);
            startPoint.Image = img;
            FlowDiagram.Model.Shapes.Add(startPoint);

            endPoint = new Shape(_bs[BasicStencilType.Circle]);
            endPoint.Width = 30;
            endPoint.Height = 30;
            endPoint.AllowScale = false;
            endPoint.AllowSnap = true;
            endPoint.ApplyTheme(Singleton.Instance.GetTheme(Themes.Green));
            endPoint.Tag = "End";
            endPoint.Move(200, 200);
            img = new PS.FlowDesigner.Image(Properties.Resources.step_stop);
            img.Location = new PointF(4, 4);
            endPoint.Image = img;
            FlowDiagram.Model.Shapes.Add(endPoint);

            errorPoint = new Shape(_bs[BasicStencilType.Circle]);
            errorPoint.Width = 30;
            errorPoint.Height = 30;
            errorPoint.AllowScale = false;
            errorPoint.AllowSnap = true;
            errorPoint.ApplyTheme(Singleton.Instance.GetTheme(Themes.Orange));
            errorPoint.Tag = "Error";
            errorPoint.Move(200, 300);
            img = new PS.FlowDesigner.Image(Properties.Resources.cross);
            img.Location = new PointF(4, 4);
            errorPoint.Image = img;
            FlowDiagram.Model.Shapes.Add(errorPoint);

            FlowDiagram.Refresh();
        }

        private void frmFlowDesigner_Resize(object sender, EventArgs e)
        {
            FlowDiagram.Refresh();
        }

        private void tsAddShape_Click(object sender, EventArgs e)
        {
            //FlowDiagram.Cursor = new Cursor("StepInsert.cur");
            frmStepData stepData = new frmStepData();
            if (stepData.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BasicStencil _bs = new BasicStencil();
                Shape shape = new Shape(_bs[BasicStencilType.RoundedRectangle]);
                shape.ApplyTheme(Singleton.Instance.GetTheme(Themes.LightBlue));
                shape.X = FlowDiagram.Width / 2 - shape.Width / 2;
                shape.Y = FlowDiagram.Height / 2 - shape.Height / 2;

                //add ports
                List<Port> topPorts = new List<Port>();

                for (int i = 0; i < 3; i++)
                {
                    topPorts.Add(new Port(PortOrientation.Top, (i * 25) + 25));
                }

                foreach (Port p in topPorts)
                {
                    p.Width = 10;
                    p.Height = 10;
                    p.BorderColor = Color.CornflowerBlue;
                    p.BorderWidth = 1;

                    shape.Ports.Add(p);
                }

                //add a bottom port
                Port bottomPort = new Port(PortOrientation.Bottom, 50F);
                bottomPort.Width = 10;
                bottomPort.Height = 10;
                bottomPort.BorderColor = Color.CornflowerBlue;
                bottomPort.BorderWidth = 1;
                shape.Ports.Add(bottomPort);

                PS.FlowDesigner.Image img = null;
                if (stepData.StepType == "System Action")
                    img = new PS.FlowDesigner.Image(Properties.Resources.system_action);
                else
                    img = new PS.FlowDesigner.Image(Properties.Resources.user_action);
                img.Location = new PointF(5, 5);
                shape.Image = img;

                PS.FlowDesigner.Label l = new PS.FlowDesigner.Label(stepData.StepLabel);
                l.Alignment = StringAlignment.Far;
                shape.Label = l;

                shape.Width = 60 + l.Size.Width;

                shape.MinimumSize = new SizeF(100, 60);

                FlowDiagram.Model.Shapes.Add(shape);

                FlowDiagram.Refresh();
            }
        }

        private void FlowDiagram_ElementInserted(object sender, ElementsEventArgs e)
        {
            //if (e.Value.GetType() == typeof(Shape))
            //{
            //    PS.FlowDesigner.Label l = new PS.FlowDesigner.Label(DateTime.Now.ToString());
            //    Shape s = (Shape)e.Value;
            //    s.Label = l;
            //}
        }

        private void tsDeleteStep_Click(object sender, EventArgs e)
        {
            List<Connector> connectorsToRemove = new List<Connector>();

            foreach (Shape s in FlowDiagram.Controller.Model.SelectedShapes().Values)
            {
                if (s == startPoint || s == endPoint || s == errorPoint)
                    continue;
                //get all connections from and to this step
                foreach (Connector c in FlowDiagram.Controller.Model.Lines.Values)
                {
                    if(c.End.DockedElement == s || c.Start.DockedElement == s)
                        connectorsToRemove.Add(c);
                }
                FlowDiagram.Model.Shapes.Remove(s.Key);
                foreach (Connector c in connectorsToRemove)
                    FlowDiagram.Model.Lines.Remove(c.Key);
            }
            
            FlowDiagram.Refresh();
        }

        private void tsNewProcess_Click(object sender, EventArgs e)
        {

        }

        private void FlowDiagram_ElementClick(object sender, EventArgs e)
        {
            controlPropertyGrid1.SelectedObject = sender;
            if (addAction != null)
            {
                if (addAction.Visible == true)
                {
                    if (Control.ModifierKeys == Keys.Control)
                    {
                        addAction.SetStep(sender as Shape, StepType.End);
                        addAction.Activate();
                    }
                    else
                        addAction.SetStep(sender as Shape, StepType.Start);
                }
            }
        }

        private void tsAddAction_Click(object sender, EventArgs e)
        {
            addAction = new frmPointSelector();
            addAction.Show(this);
        }

        public void AddAction(frmPointSelector action)
        {
            Connector con = null;
            //is this a slef connection?
            if (action.StartPoint == action.EndPoint)
            {
                Port startP = null;
                Port endP = null;
                int portIndex = 0;
                foreach (Port port in action.StartPoint.Ports.Values)
                {
                    if (portIndex == 0)
                        startP = port;
                    if (portIndex == 2)
                        endP = port;
                    portIndex++;
                }
                con = new Connector(startP, endP);
            }
            else
                con = new Connector(action.StartPoint, action.EndPoint);

            con.Avoid = true;
            con.Rounded = true;
            con.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            con.Start.Marker = new Marker(MarkerStyle.Ellipse);
            con.End.Marker = new Arrow(true);

            PS.FlowDesigner.Label conLebel = new PS.FlowDesigner.Label(action.ActionName);
            con.Label = conLebel;

            FlowDiagram.Model.Lines.Add(con);

            FlowDiagram.Refresh();
        }

        private void txSaveFlow_Click(object sender, EventArgs e)
        {
            PS.FlowDesigner.Printing.PrintDocument doc = new PrintDocument(FlowDiagram);
            doc.DefaultPageSettings.Landscape = true;

            doc.PrintPreview();
        }
    }
}